
package com.eworking.eim.broadcast;

import org.jivesoftware.smackx.ping.packet.Ping;

import com.eworking.eim.util.EIMLog;
import com.eworking.eim.util.NetUtil;
import com.eworking.eim.xmpp.XMPPConnectionManager;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class EIMHeartBeatReceiver extends BroadcastReceiver {
    
    /** 日志输出标签 */
    private static final String _TAG = EIMHeartBeatReceiver.class.getSimpleName();
    
    @Override
    public void onReceive(Context context, Intent intent) {
        XMPPConnectionManager xmppConnectionManager = XMPPConnectionManager.getInstance();
        if (xmppConnectionManager != null && xmppConnectionManager.isConnected()) {
            EIMLog.d(_TAG, "发送XMPP连接检测心跳消息");
            xmppConnectionManager.getConnection().sendPacket(new Ping());
        } else {
            EIMLog.d(_TAG, "心跳检测：与XMPP服务器断开连接");
            try {
                if (!(NetUtil.hasDataConnection(context))) {
                    return;
                }
                EIMLog.d(_TAG, "重新连接XMPP服务器");
                xmppConnectionManager = XMPPConnectionManager.getInstance();
                if (xmppConnectionManager != null) {
                    xmppConnectionManager.reconnectASync();
                }
            } catch (Exception e) {
                EIMLog.d(_TAG, "与连接XMPP连接失败：" + e.getMessage());
            }
        }
    }
}
